package com.zcy.auth.controller;

import com.zcy.auth.service.IRolePermissionService;
import com.zcy.auth.domain.RolePermission;
import com.zcy.basic.util.PageInfo;
import com.zcy.basic.util.AjaxResult;
import com.zcy.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.zcy.basic.annotation.PermissionApi;
import java.util.List;

@RestController
@RequestMapping("/rolePermission")
@Api(value = "角色-权限对应关系相关API", description = "角色-权限对应关系的增删改查")
@PermissionApi(name = "角色-权限对应关系管理", description = "角色-权限对应关系相关增删改查")
public class RolePermissionController {
    @Autowired
    public IRolePermissionService rolePermissionService;


    /**
     * 保存和修改公用的
     * @param rolePermission 传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    @ApiOperation("保存或修改角色-权限对应关系")
    @PermissionApi(name = "保存或修改角色-权限对应关系")
    public AjaxResult saveRolePermission(@RequestBody RolePermission rolePermission) {
        try {
            if (rolePermission.getId() != null)
                rolePermissionService.update(rolePermission);
            else
                rolePermissionService.add(rolePermission);
            return AjaxResult.success("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存失败");
        }
    }

    /**
     * 删除角色-权限对应关系对象信息
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    @ApiOperation("通过Id删除角色-权限对应关系")
    @PermissionApi(name = "通过Id删除角色-权限对应关系")
    public AjaxResult deleteRolePermissionById(@PathVariable("id") Long id) {
        try {
            rolePermissionService.deleteById(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /**
     * 通过Id获取 角色-权限对应关系 实体
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("通过Id获取角色-权限对应关系")
    @PermissionApi(name = "通过Id获取角色-权限对应关系")
    public AjaxResult getRolePermissionById(@PathVariable("id") Long id) {
        try {
            RolePermission rolePermission =rolePermissionService.queryById(id);
            return AjaxResult.success(rolePermission);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


    /**
     * 查看所有的角色-权限对应关系实体信息
     * @return
     */
    @GetMapping
    @ApiOperation("查询所有角色-权限对应关系")
    @PermissionApi(name = "查询所有角色-权限对应关系")
    public AjaxResult listAll() {
        try {
            List<RolePermission> list = rolePermissionService.listAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


    /**
    * 分页查询数据
    *
    * @param pageInfo 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    @ApiOperation("分页查询角色-权限对应关系")
    @PermissionApi(name = "分页查询角色-权限对应关系")
    public AjaxResult queryRolePermissionPage(@RequestBody PageInfo pageInfo) {
        try {
            PageList<RolePermission> pageList = rolePermissionService.queryPage(pageInfo);
            return AjaxResult.success(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("分页查询失败");
        }
    }

    /**
     * 批量删除
     * @param ids 角色-权限对应关系ID数组
     * @return AjaxResult
     */
    @PatchMapping
    @ApiOperation("批量删除角色-权限对应关系")
    @PermissionApi(name = "批量删除角色-权限对应关系")
    public AjaxResult deleteBatchRolePermissionByIds(@ApiParam("rolePermissionID数组") @RequestBody List<Long> ids) {
        try {
            rolePermissionService.deleteBatch(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
}
